package cn.tedu.ssm.service;

import cn.tedu.ssm.mapper.GoodMapper;
import cn.tedu.ssm.pojo.Good;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodService {
    //    public
    @Autowired
    private GoodMapper goodMapper;

    public Map<String, Object> list(Integer page, Integer size) {
        // 将页项数转换为数据限制量
        int limit = (size == null || size < 1) ? 6 : size;
        page = page == null || page < 1 ? 1 : page;
        int pageOffset = limit * (page - 1);
        List<Good> goods = goodMapper.findAll(pageOffset, limit);
        Long count = goodMapper.count();
        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        map.put("count", count);
        return map;
    }

    public Long update(Long id, String name, Float price){
        if (goodMapper.existsId(id)){
            return goodMapper.update(id, name, price);
        }
        return -1L;
    }
    public Good findById(Long id){
        return goodMapper.findById(id);
    }

    public Long save(String name, Float price){
        return goodMapper.insert(name, price);
    }

    public Long delete(Long id){
        return goodMapper.delete(id);
    }

    public Long getCount(){
        return goodMapper.count();
    }
}
